package face;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import util.*;
import objects.*;
import dispatchers.*;
import java.util.*;

public class Input extends HttpServlet{
	DBUserDispatcher userDispatcher;
	
	public void init(){
		DBManager dbManager = new DBManager("anagram", "root", "root");
		userDispatcher = new DBUserDispatcher(dbManager, "users");
	}

	public void doPost(HttpServletRequest request, 
						HttpServletResponse response) throws ServletException, IOException{
		doGet(request, response);
	}

	public void doGet(HttpServletRequest request, 
						HttpServletResponse response) throws ServletException, IOException{
		response.setContentType("text/html");
		String mail = request.getParameter("mail");
		String password = request.getParameter("password");
		String page;
		
		if(!userDispatcher.canInput(mail, password)){
			Article article = new Article(-1, "Ошибка!", "Вы ввели неверный логин/пароль.");
			request.setAttribute("article", article);
			page = "//jsp//general//reportPage.jsp";
		}else{		
			User user = userDispatcher.getUser(mail);
			HttpSession session = request.getSession(true);
			session.setAttribute("user", user);
			page = "//jsp//user//mypage.jsp";
		}
		
		RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher(page);
		requestDispatcher.forward(request, response);
	}
	
}